package PVZ;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.io.File;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.SourceDataLine;
import javax.xml.crypto.Data;

public class Start {
    BufferedImage startgame;
    Pictrue p=new Pictrue();
    public static Boolean ifmusic=false;
    public void init(){
        //创建JFrame界面,开始界面
        JFrame f = new JFrame("植物大战僵尸");
        //添加开始游戏按钮
        ImageIcon s=new ImageIcon("D:\\Morven\\IdeaProjects\\PVZ\\picture\\startgame.png");
        JButton start = new JButton(s);
        start.addActionListener(new startListner());
        // f.setLayout(new BorderLayout());
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
        p.setPreferredSize(new Dimension(1920,1200));
        start.setBounds(600,750,s.getIconWidth(),s.getIconHeight());
        f.add(start);
        f.add(p);
        try {
            startgame= ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\src\\PVZ\\start1.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            p.repaint();
        }
        f.pack();
    }
    //引用网上添加背景音乐方法为
    public static void playMusic() {// 背景音乐播放
        try {
            AudioInputStream ais = AudioSystem.getAudioInputStream(new File("D:\\Morven\\IdeaProjects\\PVZ\\src\\PVZ\\bgm.wav"));    //绝对路径
            AudioFormat aif = ais.getFormat();
            final SourceDataLine sdl;
            DataLine.Info info = new DataLine.Info(SourceDataLine.class, aif);
            sdl = (SourceDataLine) AudioSystem.getLine(info);
            sdl.open(aif);
            sdl.start();
            FloatControl fc = (FloatControl) sdl.getControl(FloatControl.Type.MASTER_GAIN);
            // value可以用来设置音量，从0-2.0
            double value = 2;
            float dB = (float) (Math.log(value == 0.0 ? 0.0001 : value) / Math.log(10.0) * 20.0);
            fc.setValue(dB);
            int nByte = 0;
            final int SIZE = 1024 * 64;
            byte[] buffer = new byte[SIZE];
            while (nByte != -1) {
                nByte = ais.read(buffer, 0, SIZE);
                sdl.write(buffer, 0, nByte);
            }
            sdl.stop();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
     class startListner implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if(Game.ifGame){
            Game.init1();
            new Thread(()->{while(ifmusic) {
                Start.playMusic();} //while中的true可换成参数来控制音乐的停止播放
            }).start();
        }}
    }
    class  Pictrue extends JPanel{
    public  void paint(Graphics g) {
        g.drawImage(startgame,0,0,null);
    }
    }
}

